package org.liveboardgames.agricola.control.commands
{
    import com.adobe.cairngorm.commands.ICommand;
    import com.adobe.cairngorm.control.CairngormEvent;
    
    import mx.controls.Alert;
    import mx.rpc.IResponder;
    import mx.rpc.events.FaultEvent;
    
    import org.liveboardgames.agricola.control.delegates.action.PerformActionDelegate;
    import org.liveboardgames.agricola.control.events.PerformActionEvent;
    import org.liveboardgames.agricola.model.AgricolaModelLocator;
    import org.liveboardgames.agricola.vo.action.Action;
    
    public class PerformActionCommand implements ICommand, IResponder
    {
    	private var action:Action;
    	
        public function execute(event:CairngormEvent):void
        {
            var delegate:PerformActionDelegate = new PerformActionDelegate(this);
            var performActionEvent:PerformActionEvent = PerformActionEvent(event);
            delegate.game = performActionEvent.game;
            delegate.action = performActionEvent.action;
            this.action = performActionEvent.action;
            delegate.executeAction(performActionEvent.actionTarget);
        }
        
        public function result(data:Object):void
        {
            trace("Action " + action + " executed");
            AgricolaModelLocator.getInstance().actionController.addNewPlayedAction(action);
        }
        
        public function fault(info:Object):void
        {
            var faultEvent:FaultEvent = FaultEvent(info);
            Alert.show("We couldn't perform the action :(", "Error");
        }
    
    }
}